package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.dao.ConstantDao;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.ConstantDto;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.in.ConstantDtoIn;
import com.zyw.zhuangdian.pojo.out.ConstantDtoOut;
import com.zyw.zhuangdian.service.ConstantService;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ConstantServiceImpl implements ConstantService {
    private final Logger logger = LoggerFactory.getLogger(ConstantServiceImpl.class);

    @Autowired
    private ConstantDao constantDao;

    @Override
    public Resp<Void> addConstant(ConstantDtoIn in) {
        Resp<Void> resp = new Resp<>();

        constantDao.addConstant(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateConstant(ConstantDtoIn in) {
        Resp<Void> resp = new Resp<>();

        constantDao.updateConstant(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteConstant(ConstantDtoIn in) {
        Resp<Void> resp = new Resp<>();

        in.setStatus(ServiceConstant.STATUS_INVALID);

        constantDao.updateConstant(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<List<ConstantDtoOut>> listConstants(ConstantDtoIn in) {
        Resp<List<ConstantDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }

        int count = constantDao.listConstantsCount(BeanConvertUtil.beanCovertMap(in));
        List<ConstantDtoOut> list = new ArrayList<>();
        if (count > 0){
            Map<String,ConstantDtoOut> map = new HashMap<>();
            List<Map> listConstant = constantDao.listConstant(BeanConvertUtil.beanCovertMap(in));
            List<ConstantDto> dtoList = BeanConvertUtil.covertBeanList(listConstant, ConstantDto.class);

            for(ConstantDto dto:dtoList){
                if (!map.containsKey(dto.getKey())){
                    ConstantDtoOut o = new ConstantDtoOut();
                    o.setKey(dto.getKey());
                    o.setKeyName(dto.getKeyName());
                    o.setCid(dto.getCid());
                    o.setValues(new ArrayList<>());
                    map.put(dto.getKey(),o);
                }
                map.get(dto.getKey()).getValues().add(dto);

            }

            list = new ArrayList<>(map.values());
        }
        resp.setData(list);

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }

        return resp;
    }
}
